<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
  >
    <!-- 权限点数据展示 -->
    <!-- show-checkbox 提供选择功能  check-strictly 关闭父子关联-->
    <el-tree
      ref="tree"
      node-key="id"
      check-strictly
      show-checkbox
      :data="permList"
      default-expand-all
      :props="{ label: 'name' }"
    ></el-tree>

    <template #footer>
      <el-button @click="closeAssignDialog">取消</el-button>
      <el-button
        type="primary"
        @click="AssignPerm"
      >确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranformTreeData } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'

export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 树形数据
      permList: [],
      // 角色ID
      roleId: null
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 分配权限
    async AssignPerm () {
      try {
        // 通过：this.$refs.tree.getCheckedKeys() 获取树形组件中选中的权限点ID数组
        await assignPerm({ id: this.roleId, permIds: this.$refs.tree.getCheckedKeys() })
        this.$message.success('分配权限点成功')
        this.$emit('update:showAssignDialog', false)
      } catch (error) {
        console.log(error)
      }
    },
    // 获取角色权限数据
    async getRolePerms (id) {
      const { permIds } = await getRoleDetail(id)
      // console.log(perms)
      // 数据回显,存储ID
      this.roleId = id
      this.$refs.tree.setCheckedKeys(permIds)
    },
    //  获取权限点列表数据
    async getList () {
      const list = await getPermissionList()
      // console.table(list)
      this.permList = tranformTreeData(list)
    },
    closeAssignDialog () {
      this.$emit('update:showAssignDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
